﻿using System;
using System.Collections.Generic;

public class WeatherStation
{
    private double temperature;
    private double humidity;
    private DateTime lastCollectionTime;
    private List<IObserver> observers = new List<IObserver>();

    public double Temperature
    {
        get { return temperature; }
        set
        {
            temperature = value;
            lastCollectionTime = DateTime.Now;
            NotifyObservers();
        }
    }

    public double Humidity
    {
        get { return humidity; }
        set
        {
            humidity = value;
            lastCollectionTime = DateTime.Now;
            NotifyObservers();
        }
    }

    public DateTime LastCollectionTime => lastCollectionTime;
    public void AddObserver(IObserver observer)
    {
        observers.Add(observer);
    }
    public void RemoveObserver(IObserver observer)
    {
        observers.Remove(observer);
    }
    private void NotifyObservers()
    {
        foreach (var observer in observers)
        {
            observer.Update(this);
        }
    }
}
public interface IObserver
{
    void Update(WeatherStation station);
}
public class ScreenDisplay : IObserver
{
    public void Update(WeatherStation station)
    {
        Console.WriteLine("=== 屏幕显示 ===");
        Console.WriteLine($"温度：{station.Temperature}°C");
        Console.WriteLine($"湿度：{station.Humidity}%");
        Console.WriteLine($"最后采集时间：{station.LastCollectionTime}");
        Console.WriteLine("===============");
    }
}
public class DatabaseSaver : IObserver
{
    public void Update(WeatherStation station)
    {
        Console.WriteLine("=== 数据库保存 ===");
        Console.WriteLine($"保存温度：{station.Temperature}°C 到数据库");
        Console.WriteLine($"保存湿度：{station.Humidity}% 到数据库");
        Console.WriteLine($"保存时间：{station.LastCollectionTime} 到数据库");
        Console.WriteLine("================");
    }
}
class Program
{
    static void Main(string[] args)
    {
        WeatherStation station = new WeatherStation();
        ScreenDisplay screen = new ScreenDisplay();
        DatabaseSaver dbSaver = new DatabaseSaver();
        station.AddObserver(screen);
        station.AddObserver(dbSaver);
        station.Temperature = 25.5;
        Console.WriteLine();

        station.Humidity = 60;
        Console.WriteLine();

        station.Temperature = 26.0;

        Console.ReadKey();
    }
}